package com.qhnu.zxjx.service.impl;

import com.qhnu.zxjx.dao.zxjx.UserMapper;
import com.qhnu.zxjx.enums.SystemCodeEnum;
import com.qhnu.zxjx.enums.UserStatusEnum;
import com.qhnu.zxjx.exception.CustomException;
import com.qhnu.zxjx.model.entity.User;
import com.qhnu.zxjx.model.vo.LoginVo;
import com.qhnu.zxjx.model.vo.RegisterVo;
import com.qhnu.zxjx.service.IUserService;
import com.qhnu.zxjx.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 用户模块service
 * @author: panpei
 * @date: 2019/11/25 11:31
 * @version: 1.0
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(RegisterVo registerVo) {
        String password = registerVo.getPassword();
        String confirmPassword = registerVo.getConfirmPassword();
        // 判断2次输入的密码是否一致
        if (!password.equals(confirmPassword)) {
            throw new CustomException(SystemCodeEnum.ERROR_PASSWORD.getCode(), "密码与确认密码不一致");
        }

        // 判断用户是否已注册
        String userId = registerVo.getUserId();
        User existUser = userMapper.selectByUserId(userId);
        if (existUser != null) {
            throw new CustomException(SystemCodeEnum.ERROR_ACCOUNT_EXIST.getCode(), "账号已存在");
        }

        // 注册
        User user = new User();
        user.setUserId(userId);
        user.setUserName(registerVo.getUserName());
        user.setPassword(MD5Util.md5(registerVo.getPassword()));
        user.setStatus(UserStatusEnum.REVIEW.getStatus());
        user.setCreator(userId);
        user.setEditor(userId);
        userMapper.insertSelective(user);
    }
}
